﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using WeiXinPayClass;
using WxPayAPI;
using Common;
using Model;
using DAL;

namespace UI
{
    public partial class notify_wx : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.RequestType == "POST")
            {
                try
                {
                    WxPayHelper helper = new WxPayHelper();
                    StreamReader reader = new StreamReader(Request.InputStream);
                    String xmlData = reader.ReadToEnd();
                    helper.ReceivePostXmlData(xmlData);
                    Dictionary<string, string> dicBack = helper.GetParameter();//获取所有参数
                    string out_trade_no = dicBack["out_trade_no"];//商户订单号
                    //common_util.WriteLog("out_trade_no=" + out_trade_no);
                    //1.验证商户订单号是否被处理
                    //2.处理过直接返回成功，否则返回
                    //此处根据out_trade_no 处理业务数据
                    //处理业务数据结束
                    string[] paras = dicBack["attach"].Split('_');
                    string ThirdNum = dicBack["transaction_id"];
                    double Money = DataConvert.ToDouble(dicBack["total_fee"]) / 100;
                    int id = DataConvert.ToInt(paras[0]);
                    int userid = DataConvert.ToInt(paras[1]);
                    int adminid = DataConvert.ToInt(paras[2]);
                    AdminInfo admin = AdminDAL.FindOneByID(adminid);
                    WxPayAPI.WxPayConfig.KEY = admin.AppKey;
                    if (helper.CheckSign())
                    {
                        if (dicBack != null && dicBack.Keys.Contains("return_code"))
                        {
                            if (dicBack["return_code"] == "SUCCESS")
                            {
                                //common_util.WriteLog("return_code=SUCCESS");
                                if (dicBack["result_code"] == "SUCCESS")
                                {
                                    //common_util.WriteLog("result_code=SUCCESS");

                                    POrderInfo order = POrderDAL.FindOneByID(id);
                                    if (order.State == 0 && Money == order.AllMoney)
                                    {
                                        POrderDAL.UpdateState(1, adminid, userid, id);
                                        List<POrderDetailInfo> detaillist = POrderDetailDAL.FindListByOrderID(adminid,userid,id);
                                        foreach (POrderDetailInfo detail in detaillist)
                                        {
                                            ProductDAL.IncSellCount(detail.Count, detail.ProductID);
                                        }
                                    }
                                    helper.SetReturnParameter("return_code", "SUCCESS");
                                    helper.SetReturnParameter("return_msg", "");
                                }
                            }
                            if (dicBack["return_code"] == "FAIL")
                            {
                                helper.SetReturnParameter("return_code", "FAIL");
                                helper.SetReturnParameter("return_msg", dicBack["return_msg"]);
                            }
                        }
                    }
                    else
                    {
                        helper.SetReturnParameter("return_code", "FAIL");
                        helper.SetReturnParameter("return_msg", "签名失败");
                    }
                    string xmlStr = helper.GetReturnXml();
                    //common_util.WriteLog("Notify_返回xml=" + xmlStr);
                    Response.ContentType = "text/xml";
                    Response.Clear();
                    Response.Write(xmlStr);
                    Response.End();
                }
                catch (Exception ex)
                {
                    //common_util.WriteLog("Notify_异常了" + ex);
                }
            }
        }
    }
}


